/**
 * 
 */
package hr.fer.zemris.annotator.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import hr.fer.zemris.annotator.exceptions.ProgramNotSelectedException;
import hr.fer.zemris.annotator.exceptions.ProgramSourceNotSelectedException;
import hr.fer.zemris.annotator.model.ApplicationParameters;
import hr.fer.zemris.annotator.model.Method;
import hr.fer.zemris.annotator.model.MethodData;
import hr.fer.zemris.annotator.model.profiling.CallGraphData;
import hr.fer.zemris.annotator.settings.ApplicationSettings;

/**
 * @author dajan
 * 
 */
public class CommandBuilderService {

	public static String getProfileCommand() {

		ApplicationParameters parameters = ApplicationParameters.getInstance();
		parameters.prepareParameters();
		boolean outputPathSelected = true;
		String outputPath = ApplicationSettings.APPLICATION_OUTPUT_PATH;

		try {
			outputPathSelected = parameters.validateParameters();
		} catch (ProgramNotSelectedException e) {

			e.printStackTrace();
			System.exit(0);
		} catch (ProgramSourceNotSelectedException e) {

			e.printStackTrace();
			System.exit(0);
		}

		if (outputPathSelected) {

			outputPath = parameters.getOutputPath();

		}

		String command = "cd ..; cd ..; cd scripts; . profile_c_code.sh "
				+ parameters.getCompiledProgramPath() + " "
				+ parameters.getCompiledProgramName() + " " + outputPath
				+ ApplicationSettings.APPLICATION_OUTPUT_PROFILING_FILE_NAME;

		return command;
	}
	
	public static String getProfilingOutputPath(){
		
		String path = ApplicationParameters.getInstance().getOutputPath();
		
		path += ApplicationSettings.APPLICATION_OUTPUT_PROFILING_FILE_NAME;
		
		return path;
		
	}
	
	public static String getStringFromFile(File file) throws IOException{
		
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String fileString = "";
		
		while(true){
			
			String line = reader.readLine();
			if(line == null) break;
			fileString += line;
			fileString += System.getProperty("line.separator");
			
		}
		reader.close();
		return fileString;
		
	}
	
	public static String getOutputFilePath(File file){
		
		String fileName = file.getName();
		fileName = fileName.replaceAll(".c", "");
		fileName += "_annotated.c";
		
		String path = ApplicationParameters.getInstance().getOutputPath();
		
		if(path.equals("")){
			path = ApplicationSettings.APPLICATION_OUTPUT_PATH;
		}
		
		return path + "/" + fileName;
		
		
	}
	
	public static String getAnnotationString(String method, MethodData methodData){
		
		String methodName = method.split("\\(")[0];
		methodName = methodName.trim();
		List<Method> methodNames = methodData.getMethodFlatData();
		String annotationString = "";
		
		for(Method meth : methodNames){
			if(meth.getMethodName().equals(methodName)){
				//todo string
				annotationString = methodData.toString(meth) + method;
			}
		}
		
		
		return annotationString;
	}
	
//	public static String getCallGraphDataFromIndex(String methodIndex, List<CallGraphData> data){
//	
//	}
	
	public static String getMemoString(){
		
//		String memoStr = "/* \n" +
//				"** This code is annotated by the Performan(C)eAnnotator, \n" +
//				"** application written by Dajan Zvekić as part of the Master Thesis \n" +
//				"** under the mentorship of prof. dr. sc. Vlado Sruk. \n" +
//				"*/ \n";
		
		
		//TODO: upgrade -- if different memos are available choose right one..
		
		return ApplicationSettings.APPLICATION_MEMO;
	}
	
	public static String getProfilingDataExplanation(int profilingData){
		
		switch (profilingData) {
		case ApplicationSettings.FLAT_PROFILING_DATA:
			
			return ApplicationSettings.FLAT_PROFILING_DATA_EXPLANATION;
		

		case ApplicationSettings.CALL_GRAPH_DATA:
			
			return ApplicationSettings.CALL_GRAPH_PROFILING_DATA_EXPLANATION;
			
		default:
			return "";
		}
		
		
		
	}
	
	
}
